<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * +----------------------------------------------------------------------
 * 部门管理
 * +----------------------------------------------------------------------
 * Author: ZF <ZFajpst_2008@126.com> 2012-10-23 10:27:00
 * +----------------------------------------------------------------------
 */
 
	class Depart extends MY_Controller {
	
		public function __construct(){
			parent::__construct();
			$this->load->model('depart_model', 'm');
		}
	
		/**
		 * +---------------------
		 * 部门管理
		 * +---------------------
		 */
		public function index(){
			$data['list'] = $this->m->index();
			$data['menu'] = $this->getMenu();
			$this->load->view('depart_index', $data);
		}
	
		/**
		 * +---------------------
		 * 部门添加
		 * +---------------------
		 */
		public function add($pid = 0, $action = ''){
			// 加载辅助类、函数
			$this->load->helper('form');
			$this->load->library('form_validation');
			
			// 初始化数据
			$data['pid'] = $pid;
			$data['list'] = $this->m->getDepart();
			$data['menu'] = $this->getMenu();
			
			// 显示、执行添加
			if($action != 'do'){
				$this->load->view('depart_add', $data);
			}else{
				$this->form_validation->set_rules('name', '部门名称', 'required');
				$this->form_validation->set_rules('ordernum', '排序序号', 'required|numeric');
				
				if($this->form_validation->run() == false){
					$this->load->view('depart_add', $data);
				}else{
					if($this->m->add()){
						echo '<script>alert("数据添加成功！");window.location = "/index.php/depart"</script>';
					}else{
						echo '<script>alert("数据添加失败！");window.history.back()</script>';
					}
				}
			}
		}
	
		/**
		 * +---------------------
		 * 部门修改
		 * +---------------------
		 */
		public function edit($id, $action = ''){
			// 加载辅助类、函数
			$this->load->helper('form');
			$this->load->library('form_validation');
			
			// 初始化数据
			$data['id'] = $id;
			$data['list'] = $this->m->getDepart();
			$data['info'] = $this->m->edit($id);
			$data['menu'] = $this->getMenu();
			
			// 显示、执行修改
			if($action != 'do'){
				$this->load->view('depart_edit', $data);
			}else{
				$this->form_validation->set_rules('name', '部门名称', 'required');
				$this->form_validation->set_rules('ordernum', '排序序号', 'required|numeric');
				
				if($this->form_validation->run() == false){
					$this->load->view('depart_edit', $data);
				}else{
					if($this->m->edit($id, 'do')){
						echo '<script>alert("数据修改成功！");window.location = "/index.php/depart"</script>';
					}else{
						echo '<script>alert("数据修改失败！");window.history.back()</script>';
					}
				}
			}
		}
		
		/**
		 * +---------------------
		 * 部门删除
		 * +---------------------
		 */
		public function del($id){
			if($this->m->del($id)){
				echo '<script>alert("数据删除成功！");window.location = "/index.php/depart"</script>';
			}else{
				echo '<script>alert("数据删除失败！");window.history.back()</script>';
			}
		}
		
		/**
		 * +---------------------
		 * 分配权限
		 * +---------------------
		 */
		public function power($id, $action = ''){
			if($action != 'do'){
				$powerArr = $this->m->power($id);
			
				$data['id'] = $id;							// 职位ID
				$data['name'] = $powerArr[0]['name'];		// 职位名称
				$data['notes'] = $powerArr[0]['notes'];		// 职位描述
				$data['plist1'] = $powerArr[1];				// 权限列表
				$data['plist2'] = $powerArr[2];				// ...
				$data['plist3'] = $powerArr[3];				// ...
				$data['havepower'] = $powerArr[99];			// 已有权限
				$data['menu'] = $this->getMenu();
				// var_dump($powerArr[99]);
				
				$this->load->view('depart_power', $data);
			}else{
				if($this->m->power($id, 'do')){
					echo '<script>alert("权限分配成功！");window.location = "/index.php/depart"</script>';
				}else{
					echo '<script>alert("权限分配失败！");window.history.back()</script>';
				}
			}			
		}
	
	}